home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
PROGRAMM
/
DB_CLIPP
/
0411.ZIP
/
DBINFO.C
< prev
next >
Wrap
Text File
|
1985-10-29
|
5KB
|
238 lines
/*
* Program to read a dBASE III .DBF file, and display the header information.
*/
#include "stdio.h"
main(argc,argv)
int argc;
char *argv[];
{
char ftype[10],
noext;
int c,
cntr,
reclen,
reccntr,
records,
numread,
outfile,
inlen,
infile;
char infname[64],
exfname[64],
inext[5];
struct db3head
{
/* byte 1 */
char version;
/* bytes 2-4 */
char year,
month,
day;
/* bytes 5-6 */
char lorecs,
hirecs;
/* bytes 7-8 */
char dummyh01[2];
/* bytes 9-10 */
char datadisp[2];
/* bytes 11-12 */
char recdlen[2];
/* bytes 13-32 */
char dummyh02[20];
};
struct db3rec
{
/* bytes 1-11 - NB 11th byte always a null */
char f_name[11];
/* byte 12 */
char f_type;
/* bytes 13-14 */
char dummyf01[2];
/* bytes 15-16 - seems to always be 0x3C56 or 3E56 */
char dummyf02[2];
/* byte 17 */
char f_len;
/* byte 18 */
char f_dec;
/* bytes 19-32 */
char dummyf03[14];
};
struct db3head header;
struct db3rec field;
if ((argc == 1) || (argv[1][0] == '?'))
{
puts("\n");
puts("Syntax: DBINFO <infile> [<outfile>]\n");
puts("where infile is the filespec for the input data.\n");
puts(" outfile is the filespec for the file to which\n");
puts(" the output is to be appended.\n");
puts("\n");
puts("The input file - a dBASE III database file - is read and\n");
puts("the file header information displayed to simulate the\n");
puts("dBASE command DISPLAY STRUCTURE.\n");
puts("\n");
puts("An extension of .DBF is assumed for the input file if\n");
puts("none is given.\n");
puts("\n");
puts("Only extensions of .DBF and .BAK are valid input file\n");
puts("extensions.\n");
puts("\n");
puts("If no output file is given, the output is routed by default\n");
puts("to the screen.\n");
puts("\n");
exit(0);
}
if (argv[1][0] == '#')
{
puts("\n");
puts("PROGRAM: DBINFO\n");
puts("Author : Peter Townsend\n");
puts("Date : 28Oct85\n");
puts("Version: 1.1\n");
exit(0);
}
inlen = strlen(argv[1]);
for(cntr = 0;cntr <= inlen;cntr++)
{
infname[cntr] = toupper(argv[1][cntr]);
}
if ((infname[inlen-4] != '.') || (inlen < 4))
{
strcat(infname,".DBF");
}
inlen = strlen(infname);
for(cntr = inlen-4;cntr <= inlen;cntr++)
{
inext[cntr-inlen+4] = infname[cntr];
}
if ((strcmp(inext,".DBF") != 0) && (strcmp(inext,".BAK") != 0))
{
printf("\nInvalid extension ... must be either .DBF or .BAK\n");
exit(1);
}
if ((infile = open(infname,0)) == -1)
{
printf("\nCannot open input file %s\n",infname);
exit(1);
}
if (argc == 2)
{
strcpy(exfname,"CON");
}
else
{
strcpy(exfname,argv[2]);
}
if (strcmp(infname,exfname) == 0)
{
printf("\nInput filename and output filename cannot be the same.\n");
exit(1);
}
if ((outfile = fopen(exfname,"a")) == -1)
{
if ((outfile = creat(exfname)) == -1)
{
printf("\nCannot open/create output file %s\n",exfname);
exit(1);
}
}
numread = read(infile,header,32);
if (numread != 32)
{
printf("\nCannot access file header in database\n");
exit(1);
}
c = header.version % 0x10;
if (c != 3)
{
printf("\nNot a dBASE III database\n");
exit(1);
}
c = header.version / 0x10;
if ((c != 8) && (c != 0))
{
printf("\nNot a dBASE III database\n");
exit(1);
}
records = header.lorecs + (header.hirecs * 16);
fprintf(outfile,"\n");
fprintf(outfile,"Structure for database : %s\n",infname);
fprintf(outfile,"Number of data records : %5d\n",records);
fprintf(outfile,"Date of last update : ");
fprintf(outfile,"%02d/%02d/%02d\n",header.month,header.day,header.year);
reccntr = 1;
fprintf(outfile,"Field Field name Type Width Dec\n");
numread = read(infile,field,32);
if (numread != 32)
{
fprintf(outfile,"Cannot access next field\'s data\n");
exit(1);
}
reclen = 1;
while (isprint(field.f_name[0]))
{
if (field.f_type == 'C')
{
strcpy(ftype,"Character");
}
else
{
if (field.f_type == 'N')
{
strcpy(ftype,"Numeric");
}
else
{
if (field.f_type == 'L')
{
strcpy(ftype,"Logical");
}
else
{
if (field.f_type == 'D')
{
strcpy(ftype,"Date");
}
else
{
if (field.f_type == 'M')
{
strcpy(ftype,"Memo");
}
else
{
strcpy(ftype,"*Unknown*");
}
}
}
}
}
reclen = reclen + field.f_len;
fprintf(outfile,"%5d %-10s %-9s ",reccntr,field.f_name,ftype);
fprintf(outfile,"%6d",field.f_len);
if (field.f_dec != 0)
{
fprintf(outfile," %5d",field.f_dec);
}
fprintf(outfile,"\n");
reccntr++;
numread = read(infile,field,32);
if (numread != 32)
{
if (records > 0)
{
fprintf(outfile,"Cannot access next field\'s data\n");
exit(1);
}
else
{
field.f_name[0] = 0;
}
}
}
fprintf(outfile,"** Total ** %6d\n",reclen);
fclose(infile);
fclose(outfile);
exit(0);
}